﻿

Public Class UnionShape


    ''' <summary>
    ''' 合并Shape
    ''' </summary>
    ''' <param name="Shp">要合并的shape文件</param>
    ''' <param name="ResultShape">合并后的shape集合</param>
    ''' <remarks></remarks>
    Public Shared Sub UnionShape(ByVal Shp As MapWinGIS.Shapefile, ByRef ResultShape As List(Of MapWinGIS.Shape))

        '把Shape临时存储
        Dim TempShapeList As New List(Of MapWinGIS.Shape)
        For i As Integer = 0 To Shp.NumShapes - 1
            TempShapeList.Add(Shp.Shape(i))
        Next

        Dim u As New MapWinGIS.Utils
        'Dim nList As New List(Of Integer)


        Dim BigShape As New MapWinGIS.Shape
        BigShape = TempShapeList.Item(0)

        'shape个数大于1的情况
        While TempShapeList.Count > 1
            TempShapeList.RemoveAt(0)
            Dim B As Boolean = False

            Dim OldShape As New MapWinGIS.Shape
            OldShape = BigShape

            Dim j As Integer
            For j = 0 To TempShapeList.Count - 1

                Dim TempShape As New MapWinGIS.Shape
                TempShape = TempShapeList.Item(j)

                BigShape = u.ClipPolygon(MapWinGIS.PolygonOperation.UNION_OPERATION, BigShape, TempShape)

                If BigShape IsNot Nothing Then
                    B = True '一旦存在相交情况，则跳出循环
                    Exit For
                Else
                    BigShape = OldShape '没有相交，赋回原来的值，进行下一次循环
                End If
            Next

            If B = True Then
                TempShapeList.RemoveAt(j) '如果两个shape合并成了一个，则删除原来的
                TempShapeList.Add(BigShape) '添加新生成的，再进行循环比较
            Else
                ResultShape.Add(BigShape) '一次循环完毕没有相交，则不存在相交，添加到结果

            End If

            BigShape = TempShapeList.Item(0) '把第一个shape赋给BigShape

        End While

        'shape个数等于1的情况或者前面shape删除完的个数等于1的情况
        If TempShapeList.Count = 1 Then
            ResultShape.Add(TempShapeList.Item(0))
        End If


    End Sub


End Class
